package org.example.demo;
import java.sql.*;

public class DBtext {
    public static void main(String[] args) {
        String url = "jdbc:mysql://127.0.0.1:3306/library_mangement?useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true";
        String user = "root";
        String password = "wyf050925";
        String sql = """
            select
                id as library_id,
                email
            from user
            where username = ? and password = ?
        """;
        int libraryId = 0;
        String email = null;
        ResultSet rs = null;
        PreparedStatement prep = null ;
        Connection conn = null;

        try {
            String name = "admin";
            String password2 = "admin123";
            Class.forName("com.mysql.cj.jdbc.Driver");
            conn = DriverManager.getConnection(url, user, password);
            prep =  conn.prepareStatement(sql);
            prep.setString(1,name);
            prep.setString(2,password2);
            prep.executeQuery();

            rs = prep.getResultSet();
            while(rs.next()){
                 libraryId = rs.getInt("library_id");
                 email = rs.getString("email");
            }
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally{
            if(rs != null){
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(prep != null){
                try {
                    prep.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }

        System.out.println(libraryId);
        System.out.println(email);


    }
}
